package com.qk.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.mapper.CoursesMapper;
import com.qk.service.CoursesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CoursesServiceImpl implements CoursesService {

    @Autowired
    private CoursesMapper coursesMapper;

    @Override
    public PageResult<Course> pageCourses(String name, String subject, String target, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Course> courses = coursesMapper.selectListByCondition(name, subject, target);
        PageInfo<Course> pageInfo = new PageInfo<>(courses);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void removeCourseById(Long id) {
        coursesMapper.deleteCourseById(id);
    }

    @Override
    public void saveCourse(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        coursesMapper.insertCourse(course);
    }

    @Override
    public Course getCourseById(Long id) {
        return coursesMapper.selectById(id);
    }

    @Override
    public void updateCourse(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        coursesMapper.updateCourse(course);
    }

    @Override
    public List<Course> listAllCourses() {
        return coursesMapper.selectListByCondition(null, null, null);
    }

    @Override
    public List<Course> listCoursesBySubject(String subject) {
        return coursesMapper.selectListBySubject(subject);
    }
}
